package com.datastructure.test.incrementmostlongchildrenlist;

import java.util.ArrayList;
import java.util.List;

public class IncrementChildSerial {
    public static void main(String[] args) {
        IncrementChildSerial incrementChildSerial = new IncrementChildSerial();
        int [] data= {2,1,5,3,6,4,8,9,7};
        incrementChildSerial.LIS(data);
    }
    public int[] LIS (int[] arr) {
        // write code here
        if( arr.length<2){
            return arr;
        }
        //存放着数组每个位置最大递增子序列长度
        int[] dp = new int[arr.length];
        dp[0] = 1;


        int maxans = 1;
        //是一种从后往前找的思想
        for (int i = 1; i < arr.length; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (arr[i] > arr[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            maxans = Math.max(maxans, dp[i]);
        }
        // 寻找最小字典序
        // 从后往前拿
        int [] res = new int[maxans];
        int i = arr.length-1;

        for(;i>=0 && maxans >0; i--){
            if(dp[i]==maxans){
                res[maxans-1] = arr[i];
                maxans--;
            }
        }

        return res;
    }
}
